(function(){
    let open = XMLHttpRequest.prototype.open
    XMLHttpRequest.prototype.open = function(){
        let arg = arguments
        arg[0] = 'GET'
        open.apply(this, arg)
    }
})()
var app = angular.module("TMSWeb", ["kendo.directives", "oc.lazyLoad"]);
app.config(function ($controllerProvider) {
    app.cp = $controllerProvider;
});

app.controller("SideController", ['$scope', '$rootScope', function ($scope, $rootScope) {

    var path = window.location.pathname;
    $scope.sideMenu = $("#side-menu").kendoPanelBar().data("kendoPanelBar");
    var menus = $("#side-menu a");
    for (var i = 0; i < menus.length; i++) {
        if (path == $(menus[i]).attr("href")) {
            var select = $(menus[i]).closest("li.k-item");
            var expand = select.parent().closest("li.k-item");
            $scope.sideMenu.expand(expand);
            $scope.sideMenu.select(select);
            break;
        }
    }
}]);

app.controller("SiteController", ['$scope', function ($scope) {
    var sorgId = Cookies.get("CURRENT_SITE")||$("#page-site").val();
    $("#page-site").kendoDropDownTreeView({
        value: Cookies.get("CURRENT_SITE"),
        treeView: {
            dataSource: getHierarchicalDataSource("organizationId",
                contextPath + '/sysOrganization/findTreeByParentId.json', null,
                "children"),
            dataTextField: "name"
        }
        , valueField: "organizationId", textField: "name",
        change: function (e) {
            var orgId = $("#page-site").val();
            if (sorgId != orgId) {
                Cookies.set('CURRENT_SITE', orgId, {path: "/"});
                location.reload();
            }
        }
    }).data("kendoDropDownTreeView").value(Cookies.get("CURRENT_SITE") || sorgId);
}]);

app.directive('ngName', [function () {
    return {
        priority: 1,//优先级
        terminal: false,//是否终止其他绑定
        link: function (scope, element, attr) {
            var $name = $(element).siblings("span.k-state-default").children(".k-input");
            $(element).change(function () {
                var text = $name.val() || $name.text();
                scope.$eval(attr.ngName + "=" + "'" + text + "'");
                scope.$apply();
            })
        }
    };
}]);
/**
 * 计算宽度
 */
app.directive('kWidth', [function () {
    return {
        priority: 0,//优先级
        terminal: false,//是否终止其他绑定
        link: function (scope, element, attr) {
            var $name = $(element);
            var width = $(element).attr("k-width");
            if (width >= 1000) {
                width = Math.max($(window).width() * 0.9, width);
            } else if (width >= 900) {
                width = Math.max($(window).width() * 0.8, width);
            } else {
                width = Math.max($(window).width() * 0.6, width);
            }

            $(element).closest(".k-window").css({width: width});
            // $(element).css({width: width-32});
        }
    };
}]);

app.directive('kendoDialog', [function () {
    return {
        priority: 1,//优先级
        terminal: false,//是否终止其他绑定
        link: function (scope, element, attr) {
            var dialog = $(element).data("kendoDialog");
            dialog.bind("open", function () {
                $(element).show();
                $(element).removeClass('hidden');
            });
        }
    };
}]);
app.directive('kendoWindow', [function () {
    return {
        priority: 1,//优先级
        terminal: false,//是否终止其他绑定
        link: function (scope, element, attr) {
            var dialog = $(element).data("kendoWindow");
            dialog.bind("open", function () {
                $(element).show();
                $(element).removeClass('hidden');
            });
        }
    };
}]);
app.directive('ngHidden', function () {
    return {
        restrict: 'AE', //attribute or element
        scope: {},
        replace: true,
        require: 'ngModel',
        link: function ($scope, elem, attr, ngModel) {
            $scope.$watch(ngModel, function (nv) {
                elem.val(nv);
            });
            elem.change(function () { //bind the change event to hidden input
                $scope.$apply(function () {
                    ngModel.$setViewValue(elem.val());
                });
            });
        }
    };
});
app.directive('kendoComboBox', [function () {
    return {
        priority: 2,//优先级
        terminal: false,//是否终止其他绑定
        link: function (scope, element, attr) {
            var that = $(element).data("kendoComboBox");
            $(element).siblings("span").children("input.k-input").keyup(function (event) {
                that.search($(this).val());
            });
            that.bind("change", function (e) {
                var v = $(that.element).val();
                if (v && that.dataSource && that.dataSource && that.dataSource.data().length > 0) {
                    var data = $.grep(that.dataSource.data(), function (item, i) {
                        return item[that.options.dataValueField] == v;
                    });
                    if (data.length < 1) {
                        that.value("");
                        $(that.element).trigger("change");
                    }
                }
            })
        }
    };
}]);
app.directive('kendoAutoComplete', [function () {
    return {
        priority: 2,//优先级
        terminal: false,//是否终止其他绑定
        link: function (scope, element, attr) {
            var that = $(element).data("kendoAutoComplete");
            $(element).keyup(function (event) {
                that.search($(this).val());
                /*var orgData = that.dataSource.data();根据多列搜索
                 var v =$(this).val();
                 var data  =$.grep(orgData,function(item,i){
                 return item.vehicleNo.indexOf(v)>-1||item.driver.indexOf(v)>-1
                 });
                 that.setDataSource(new kendo.data.DataSource({
                 data: data
                 }));*/
            });
        }
    };
}]);


(function () {


    var utils = {};

    var nargs = /\{([0-9a-zA-Z_]+)\}/g;

    utils.template = function (string) {
        var args;

        if (arguments.length === 2 && typeof arguments[1] === "object") {
            args = arguments[1];
        } else {
            args = new Array(arguments.length - 1);
            for (var i = 1; i < arguments.length; ++i) {
                args[i - 1] = arguments[i];
            }
        }

        if (!args || !args.hasOwnProperty) {
            args = {};
        }

        return string.replace(nargs, function replaceArg(match, i, index) {
            var result;

            if (string[index - 1] === "{" &&
                string[index + match.length] === "}") {
                return i;
            } else {
                result = args.hasOwnProperty(i) ? args[i] : null;
                if (result === null || result === undefined) {
                    return "";
                }

                return result;
            }
        });
    };

    // url("abc/{0}?q={1}", 123, "the sec params") ==> "abc/123?q=the sec params"
    // url("abc/{id}, {id:123}) ==> "abc/123"
    utils.url = function (path) {
        if (contextPath) path = contextPath + "/" + path;
        return utils.template(path, Array.prototype.slice.call(arguments, 1));
    };

    window.utils = utils;

    let linkAs = $('#side-menu').find('a')
    for(let item of linkAs) {
        let href = item.getAttribute('href')
        href = href.replace(/\//g, '_')
        item.href = '/fake_web/' + href.replace('_', '')
    }
})();

